package com.uyaer.wheel
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.game.map.RandomMap;
	import com.uyaer.wheel.res.ResManager;
	
	import flash.geom.Point;
	
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.phys.Material;
	import nape.shape.Circle;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class Player extends Sprite
	{
		public static const NORMAL:String = "normal";
		public static const POPING:String = "poping";
		public static const START_POP:String = "start_pop";
		public static const END_POP:String = "end_pop";
		public static const NO_CONTROL:String = "no_control";
		
		public var body:Body;
		private var _state:String;

		private var popImg:Image;

		private var ball:MovieClip;

		private var endToPoint:Point;
		public function Player()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
			
		}
		
		private function onAdded(e:Event = null):void{
			this._state = NORMAL;
			
			body = new Body(BodyType.DYNAMIC,new Vec2(Common.W/2,Common.H/2));
			body.shapes.add(new Circle(16,null,Material.rubber()));
			body.mass = 1.78;
			body.userData.data = {"texture":this};
			body.cbTypes.add(Common.meCbType);
			body.space = Common.space;
			
			ball = new MovieClip(ResManager.me.getTextures("ball_c1"), 6);
			addChild(ball);
			Starling.juggler.add(ball);
			
			popImg = new Image(ResManager.me.getTexture("ball_pop"));
			popImg.alpha = 0;
			popImg.x = (ball.width-popImg.width)*0.5;
			popImg.y = (ball.height-popImg.height)*0.5;
			addChild(popImg);
			
			this.pivotX = ball.width>>1;
			this.pivotY = ball.height >>1;
			
			stage.addEventListener(EventType.EAT_POP,onEatPopHandler);
			stage.addEventListener(EventType.POP_OVER,onPopOverHandler);
		}
		
		private function onPopOverHandler():void{
			_state = END_POP;	
			body.space = null;
			endToPoint = RandomMap.instance.getCenterEmptyPos();
			var d:Number = Point.distance(endToPoint,new Point(body.bounds.x,body.bounds.y));
			Starling.juggler.tween(this,d/100,{x:endToPoint.x+ball.width*0.5,y:endToPoint.y+ball.height*0.5
				,"transition":Transitions.EASE_OUT
				,"onComplete":popEndToPosHandler});
		}
		
		private function popEndToPosHandler():void{
			this.x = endToPoint.x+ball.width*0.5;
			this.y = endToPoint.y+ball.height*0.5;
			body.position.setxy(this.x,this.y);
			body.shapes.at(0).sensorEnabled = false;
			body.space = Common.space;
			_state = NORMAL;
			var index:int = parent.getChildIndex(this);
			this.parent.swapChildrenAt(index,0);
			popImg.alpha = 0;
		}
		
		private function onEatPopHandler():void{
			if(this.state == NORMAL){
				this._state = START_POP;
				body.shapes.at(0).sensorEnabled = true;
				var index:int = parent.getChildIndex(this);
				this.parent.swapChildrenAt(index,parent.numChildren-1);
				
				Starling.juggler.tween(popImg,1,{"alpha":1,"transition":Transitions.EASE_OUT,
						"onComplete":startPopOver});
			}else if(this.state == POPING){
				stage.dispatchEventWith(EventType.POPING);
			}
		}
		
		private function startPopOver():void{
			_state = POPING;
			stage.dispatchEventWith(EventType.POPING);
		}
		
		public function updateDy():void{
			switch(state){
				case POPING:
					if(this.x<0){
						body.velocity.x *=-1;
					}else if(this.x>Common.W){
						body.velocity.x *=-1;
					}
					break;
			}
		}
		
		public function destory():void{
			Starling.juggler.remove(ball);
			body.space.bodies.remove(body);
			body.userData.data = null;
			body.cbTypes.clear();
			body.shapes.clear();
			body = null;
			super.dispose();
			this.removeFromParent(true);
		}

		public function get state():String{
			return _state;
		}
	}
}